<template>
  <div class="search_ordinary" v-if="searchConfig.length">
    <div class="search_ordinary_container">
      <el-form @submit.native.prevent class="search_select">
        <el-select v-model="searchItem"
                   value-key="searchName"
                   placeholder="请选择搜索列"
                   class="select_label"
                   @change="ordinaryData = searchItem.searchValue"
                   size="small">
          <el-option
            v-for="(item, index) in searchConfig"
            :key="index"
            :label="item.label"
            :value="item">
          </el-option>
        </el-select>
        <div class="ordinary_data">
          <div class="ordinary_input"
               v-for="item in searchConfig"
               :key="item.searchName"
               v-if="item.searchName === searchItem.searchName">
            <el-input v-model.trim="ordinaryData"
                      class="search_wrapper"
                      v-if="item.searchType === 'input'"
                      :placeholder="'请输入' + item.label"
                      size="small"
                      clearable>
            </el-input>
            <el-select v-model="ordinaryData"
                       class="search_wrapper"
                       v-else-if="item.searchType === 'select'"
                       :placeholder="'请选择' + item.label"
                       :multiple="item.searchMultiple"
                       :collapse-tags="item.searchMultiple"
                       size="small">
              <el-option
                v-for="item1 in item.selectList"
                :key="item1.id"
                :label="item1.label"
                :value="item1.id">
              </el-option>
            </el-select>
            <el-date-picker
              v-model="ordinaryData"
              v-else-if="item.searchType === 'date'"
              class="search_wrapper"
              type="daterange"
              size="small"
              align="right"
              unlink-panels
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :default-time="['00:00:00', '23:59:59']"
              value-format="yyyy-MM-dd HH:mm:ss">
            </el-date-picker>
          </div>
          <div class="mes_btn">
            <el-button type="primary"
                       native-type="submit"
                       @click="dataFormat"
                       class="search_btn" icon="el-icon-search"></el-button>
          </div>
        </div>
      </el-form>
      <code-scan
        v-if="codeScan"
        @codeSubmit="handleCodeScan">
      </code-scan>
      <div class="search_result" v-if="!isNaN(searchResult)">已查询出{{searchResult}}条结果</div>
    </div>
    <slot></slot>
  </div>
</template>

<script>
import dictConfig from './dictConfig'

export default {
  name: 'ordinarySearch',
  props: {
    searchList: {
      // 搜索配置列表
      type: Array,
      required: true
    },
    searchResult: {
      // 搜索结果
      type: Number,
      default: NaN
    },
    codeScan: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      // 搜索内容
      searchInfo: {},
      // 当前搜索项
      searchItem: {},
      // 普通搜索的值
      ordinaryData: ''
    }
  },
  computed: Object.assign({
    // 搜索项列表
    searchConfig() {
      return this.searchList.map(item => {
        item.searchName = item.searchName || item.prop || item.name
        item.searchMultiple = item.searchMultiple || item.multiple || false
        item.searchValue = item.searchValue || item.value
        item.searchDict = item.searchDict || item.dictType
        item.searchType = item.searchType || item.type
        if (item.searchMultiple === true && item.searchDict) {
          item.selectList = item.selectList || this[item.searchDict]
        } else if (item.searchDict) {
          item.selectList = item.selectList || this.addLabelAll(this[item.searchDict])
        }
        return item
      })
    }
  }, dictConfig),
  mounted() {
    this.clean()
  },
  methods: {
    // 清空所有搜索条件
    clean() {
      if (typeof this.searchConfig[0] === 'object') {
        this.searchItem = this.searchConfig[0]
        this.ordinaryData = this.searchItem.searchValue
      } else {
        this.searchItem = {}
        this.ordinaryData = ''
      }
    },
    // 普通搜索查询 及 数据格式化
    dataFormat() {
      this.searchInfo = {}
      if (this.ordinaryData && this.ordinaryData.length > 0) {
        switch (this.searchItem.searchType) {
          case 'input':
            this.searchInfo[this.searchItem.searchName] = this.ordinaryData
            break
          case 'select':
            if (this.searchItem.searchMultiple) {
              this.searchInfo[this.searchItem.searchName] = this.ordinaryData.join(',')
            } else {
              this.searchInfo[this.searchItem.searchName] = this.ordinaryData
            }
            break
          case 'date':
            let str = this.searchItem.searchName.slice(0, 1).toLocaleUpperCase() + this.searchItem.searchName.slice(1)
            this.searchInfo['begin' + str] = this.ordinaryData[0]
            this.searchInfo['end' + str] = this.ordinaryData[1]
            break
        }
      }
      this.$emit('ordinarySearch', this.searchInfo)
    },
    handleCodeScan(value) {
      this.$emit('codeSubmit', value)
    }
  }
}
</script>
<style lang="less">
  .search_ordinary {
    .el-input__inner {
      border-color: @mesSearchInputBorCol;
      border-radius: 0px;
    }
    .select_label {
      .el-input__inner {
        border-right: 0;
        line-height: 31px;
      }
    }
    .search_select {
      .el-select .el-input.is-focus .el-input__inner {
        border-color: @mesSearchInputBorCol;
      }
      .el-input__inner:focus {
        border-color: @mesSearchInputBorCol;
      }
      .el-select, .el-date-editor {
        width: 360px;
      }
      .el-date-editor.is-active:hover {
        border-color: @mesSearchInputBorCol;
      }
      .search_btn {
        border-left: 0;
        border-top-left-radius: 0px;
        border-bottom-left-radius: 0px;
      }
    }
  }
</style>
<style scoped lang="less">
  .search_ordinary {
    margin: 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .search_ordinary_container{
      display: flex;
      align-items: center;
    }
    .other_btn {
      display: flex;
      align-items: center;
      .senior_btn {
        font-size: 14px;
        color: @mesSearchSeniorBtn;
        cursor: pointer;
        .iconfont{
          font-size: 12px;
        }
      }
      .handle_btn_cont{
        display: flex;
      }
      .set_icon{
        height: 30px;
        line-height: 30px;
        width: 30px;
        text-align: center;
        border: 1px solid @mesSearchSetIconBor;
        margin-left: 15px;
        cursor: pointer;
        color: @mesSearchSetIcon;
      }
    }
    .search_select {
      display: flex;
      align-items: center;
      flex: 0 0 520px;
      .select_label {
        flex: 0 0 165px;
        .el-input__inner {
          border-right: 0;
          border-radius: 0px;
        }
      }
      .ordinary_data {
        flex: 0 0 340px;
        height: 32px;
        display: flex;
        align-items: flex-start;
        .ordinary_input {
          flex: 1;
          .search_wrapper{
            width: 100%;
          }
        }
      }
    }
    .search_result{
      font-size: 14px;
      margin-left: 10px;
      color: @mesSearchResult;
    }
  }
</style>
